using System;
using System.Collections.Generic;
using Sce.Pss.Core.Graphics;
using Sce.Pss.HighLevel.GameEngine2D;
using Sce.Pss.HighLevel.GameEngine2D.Base;
using Sce.Pss.Core;
using Sce.Pss.Core.Environment;

using Sce.Pss.Core.Imaging;
using Sce.Pss.Core.Input;

namespace RedSkies
{
	public class WorldView
	{
		private readonly Camera2D camera;
		private readonly SpriteUV background;
		private readonly IWorldMatrix matrix;
		
		public WorldView (Camera2D camera, SpriteUV background, IWorldMatrix matrix)
		{
			this.camera = camera;
			this.background = background;
			this.matrix = matrix;
		}
		
		public void UpdateWorld(Rectangle oldRect, Rectangle newRect)
		{		
			ScrollToRightWhenNeeded (oldRect, newRect);
			
			ScrollToLeftWhenNeeded (oldRect, newRect);			
		}		
		
		bool ShouldBeScrolledToLeft (Rectangle rectOld, Rectangle rectNew, Vector2 v)
		{
			return rectNew.X<rectOld.X && rectNew.X<(v.X-200)+50;
		}

		bool ShouldBeScrolledToRight (Rectangle rectOld, Rectangle rectNew, Vector2 v)
		{
			return rectNew.X>rectOld.X && rectNew.X>(v.X+200)-50;
		}

		void ScrollToRightWhenNeeded (Rectangle rectOld, Rectangle rectNew)
		{
			Vector2 v = camera.Center;
			if (ShouldBeScrolledToRight (rectOld, rectNew, v)) 
			{				
				Vector2 vBack = background.Position;
				
				float neuXDelta = rectNew.X-rectOld.X;
				v.X = v.X + neuXDelta;
				
				camera.Center=v;
				background.Position = new Vector2(vBack.X + neuXDelta, vBack.Y);
				
				float xOutOfScreenLeft = v.X - (960/2);
				float yOnScreenStart = v.Y - (544/2);
									
				int xMatrixOutOfScreen = (int) (xOutOfScreenLeft / this.matrix.TileSize);
				int yMatrixOnScreenStart = (int) (yOnScreenStart / this.matrix.TileSize);
				int yMatrixOnScreenMax = yMatrixOnScreenStart+ (544 / this.matrix.TileSize);
				
				matrix.RemoveAllBetween(xMatrixOutOfScreen-2,xMatrixOutOfScreen,yMatrixOnScreenStart, yMatrixOnScreenMax);
				matrix.AddWorldspriteBetween(xMatrixOutOfScreen+(960/matrix.TileSize),xMatrixOutOfScreen+(960/matrix.TileSize)+5,yMatrixOnScreenStart, yMatrixOnScreenMax);
				
			}
		}

		void ScrollToLeftWhenNeeded (Rectangle rectOld, Rectangle rectNew)
		{
			Vector2 v = camera.Center;
			if (ShouldBeScrolledToLeft (rectOld, rectNew, v)) 
			{
				Vector2 vBack = background.Position;

				float neuXDelta = rectNew.X-rectOld.X;
				v.X = v.X + neuXDelta;
				camera.Center=v;
				background.Position = new Vector2(vBack.X + neuXDelta, vBack.Y);				
				
				float xOutOfScreenLeft = v.X + (960/2);
				float yOnScreenStart = v.Y - (544/2);
									
				int xMatrixOutOfScreen = (int) (xOutOfScreenLeft / this.matrix.TileSize);
				int yMatrixOnScreenStart = (int) (yOnScreenStart / this.matrix.TileSize);
				int yMatrixOnScreenMax = yMatrixOnScreenStart+ (544 / this.matrix.TileSize);
				
				matrix.RemoveAllBetween(xMatrixOutOfScreen+2,xMatrixOutOfScreen+4,yMatrixOnScreenStart, yMatrixOnScreenMax);
				matrix.AddWorldspriteBetween(xMatrixOutOfScreen-(960/matrix.TileSize)-5,xMatrixOutOfScreen-(960/matrix.TileSize)+1,yMatrixOnScreenStart, yMatrixOnScreenMax);
				
			}
		}
	}
}

